<?php

class MapService extends AbstractService
{

    protected function _init()
    {
	$this->setMapper(new MapMapper());
    }

    public function getByReview(ReviewModel $model)
    {
	if (!$model->getId()) {
	    return false;
	}

	$cacheSlot = new CacheSlotsReviewFeatures($model, 'maps');
	if (($maps = $cacheSlot->load()) === false) {
	    $maps = $this->getMapper()->getAllFeatures($model->getId(), REVIEW_TABLE, MAP_TABLE);
	    if (!$maps) {
		$maps = array();
	    }
	    $cacheSlot->setData($maps)->save();
	}
	$model->setMaps($maps);
    }

    public function getByOpinion(OpinionModel $model)
    {
	if (!$model->getId()) {
	    return false;
	}

	$cacheSlot = new CacheSlotsOpinionFeatures($model, 'maps');
	if (($maps = $cacheSlot->load()) === false) {
	    $maps = $this->getMapper()->getAllFeatures($model->getId(), OPINION_TABLE, MAP_TABLE);
	    $cacheSlot->setData($maps)->save();
	}

	if ($maps) {
	    $model->setMaps($maps);
	}
    }
    public function removeRelations($id)
    {
	if (!$id) {
	    return false;
	}
	$this->getMapper()->removeRelations($id);
    } 

    public function onBeforeRemove(AbstractModel $model)
    {
	if ($model->getId()) {
	    $this->removeRelations($model->getId());
	}
	return parent::onBeforeRemove($model);
    }
    public function onAfterSave(AbstractModel $model = null)
    {	
	if ($model->getId()) {
	    $cacheSlot = new CacheSlotsPk($this->getMapper()->getTableName(), $model->getId());
	    $cacheSlot->delete();
	}
	return true;
    }

}